﻿using DFGK.IRepository;
using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net.Mime;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 托盘
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class PalletController : BaseController
    {
        private readonly IPalletService palletService;
        private readonly IPalletRepository palletRepository;

        public PalletController(IWebHostEnvironment environment, IPalletService palletService,IPalletRepository palletRepository) : base(environment)
        {
            this.palletService = palletService;
            this.palletRepository = palletRepository;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddPallet(Pallet dto)
        {
            if (string.IsNullOrEmpty(dto.PalletBarcode))
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string message = "";
            dto.CreateUserId = base.BaseGetNowUserId();
            dto.CreateUserName = base.BaseGetNowUserName();
            bool isko = palletService.AddPallet(dto,out message);
            if (isko)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！"+message, "500"));
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpdPallet(Pallet dto)
        {
            if (dto.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            if (string.IsNullOrEmpty(dto.PalletBarcode))
            {
                return Ok(BaseResult.Result("400", "编码不能为空！"));
            }
            string message = "";
            dto.ModifyUserId = base.BaseGetNowUserId();
            dto.ModifyUserName = base.BaseGetNowUserName();
            bool isko = palletService.UpdPallet(dto,out message);
            if (isko)
            {            
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！"+message, "500"));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult DelPallet(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool isOk = palletService.DelPallet(id);
            if (isOk)
            {          
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
        /// <summary>
        /// 批量删除托盘
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        [HttpDelete]
        public ActionResult DelPalletList(long[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var ids in idList)
            {
                bool isko = palletService.DelPallet(ids);
              
                if (!isko)
                {
                    daRes = false;
                    break;
                }
            }

            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }
        /// <summary>
        /// 托盘分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="incell"></param>
        /// <param name="type"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetPallets(string? name, int incell = 0, int type = -1, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount = 0;
            var list = palletService.GetPallets(name, type, incell, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetPalletByid(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var pojb = palletService.GetPalletByid(id);
            if(pojb!=null)
                return Ok(BaseResult.Result(pojb, "查询成功", "200"));
            return Ok(BaseResult.Result(null, "查询失败", "500"));
        }
   
        /// <summary>
        /// 批量添加托盘
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BatchPalletForm(BatchPalletDTO m)
        {
            if (m.Num <= 0 || string.IsNullOrEmpty(m.BeginBarcode))
            {
                return Ok(BaseResult.Result("400", "参数有误！"));
            }
            bool result = palletService.AddPalletList(m, out string message);
            if (result)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, message, "500"));
        }

        /// <summary>
        /// 获取枚举托盘类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetEmPallType()
        {
            var list = BaseGetEnum.GetCodeRulesList<EmPalletType>();

            return Ok(list);
        }

        /// <summary>
        /// 导出 供打印使用
        /// </summary>
        /// <returns></returns>
        [HttpGet,Obsolete]
        public FileContentResult ExportToExcelPallet()
        {
            var cellList = palletRepository.GetAllEntityList().Select(s => new { s.PalletBarcode }).ToList();
            var keyValuePairs = new Dictionary<string, string>()
            {
                {"PalletBarcode","编码" },
            };
            var fileBytes = Tool.ExportToExcel(cellList, keyValuePairs);
            return File(fileBytes, MediaTypeNames.Application.Octet, "托盘吗.xlsx");
        }
    }
}
